<?php

class Core_Backend_Component_Website_Controller_Menu extends Dcmp_Component_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function indexAction($pageindex = 1, $pagesize = 20, $sort = 'id', $dir = 'asc', $areaid = 0) {
        $this->getHistory()->record();
        $areas = App::getHelper('global')->getAreas(TRUE, FALSE);
        if (!$areaid) {
            $areaid = $areas->getFirstItem()->getId();
        }
        $collection = App::getModel('website/collection_menu')
                        ->sortBy($sort, $dir)
                        ->filterByArea($areaid)
                        ->pagination($pageindex, $pagesize)
                        ->load();
        $records = $collection->loadCount();
        return $this->viewResult(array('list' => $collection, 'records' => $records, 'areas' => $areas, 'current_area_id' => $areaid));
    }

    public function createAction($areaid) {
        $area = App::getModel('global/area')->load($areaid);
        $parentMenus = App::getModel('website/collection_menu')
                        ->filterByArea($areaid)
                        ->load();
        return $this->viewResult(array('area' => $area, 'parent_menus' => $parentMenus));
    }

    public function editAction($id) {
        $model = App::getModel('website/menu')->load($id);
        $args = $this->createAction($model->getAreaId());
        $args->getParentMenus()->removeItemById($id);
        return $args->setModel($model);
    }

    public function sortAction($parent) {
        if ($parent == 0) {
            $menus = App::getModel('website/collection_menu')->where()->eq('parent_id', 0)->closest()->load();
        } else {
            $menus = App::getModel('website/menu')->setId($parent)->getChildren();
        }
        $area = App::getModel('global/area')->load($menus[0]->getAreaId());
        return $this->viewResult(array('area' => $area, 'menus' => $menus));
    }

    public function sortPostAction() {
        $sorting = $this->getRequest()->getForm('sorting');
        $table = App::getModel('website/menu')->getTable();
        foreach ($sorting as $index => $id) {
            $table->updateSorting($id, $index + 1);
        }
        return $this->redirectResult($this->getHistoryUrl('website/menu/index'));
    }

    public function applyPostAction() {
        return $this->saveModel();
    }

    public function savePostAction() {
        return $this->saveModel(TRUE);
    }

    private function saveModel($return = FALSE) {
        $data = $this->getRequest()->getForm('menu');
        $model = App::getModel('website/menu');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data)->setCreatorId(Kd_Web::getIdentity()->getId());
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            $this->getMessager()->addInform($this->__f('share.message.saveSuccess', '{website.label.menu}', $model->getTitle()));
            return $return ?
                    $this->redirectResult($this->getHistoryUrl('website/menu/index')) :
                    $this->redirectResult($this->getUrl(array('id' => $model->getIdValue()), 'edit'));
        } else {
            $this->getMessager()->addError($this->__f('share.message.saveFail', '{website.label.menu}', $model->getTitle()))
                    ->addError($errors);
            return $this->createAction()->setModel($model);
        }
    }

}
